﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

using SIS.BLL;
using SIS.ENT;

public partial class student_fee : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void btnFDSubmit_Click(object sender, EventArgs e)
    {
        Page.Validate("StudentFeeDetail");
        if (Page.IsValid)
        {
            StudentFeeDetail _detail = new StudentFeeDetail();
            _detail.SFDKey = Convert.ToInt32(lblSFDKey.Text);
            _detail.StudentFeeKey = Convert.ToInt32(txtStudentFeeKey.Text);
            _detail.Description = txtFDDescription.Text;
            decimal _amount = 0.0M;
            if (decimal.TryParse(txtFDAmount.Text, out _amount))
            {
                _detail.Amount = _amount;
            }

            decimal _addAmount = 0.0M;
            if (decimal.TryParse(txtFDAddAmount.Text, out _addAmount))
            {
                _detail.AdditionalAmount = _addAmount;
            }

            decimal _otherAmount = 0.0M;
            if (decimal.TryParse(txtFDOtherAmount.Text, out _otherAmount))
            {
                _detail.OtherAmount = _otherAmount;
            }

            _detail.DueDate = DateTime.Now.AddDays(14);
            _detail.FinalDueDate = DateTime.Now.AddDays(16);

            _detail.Status = true;

            if (StudentFeeDetailManager.Save(_detail) > 0)
            {
                ClearFDFields();
                grvStudentFeeDetail.DataBind();
                lblFDMessage.Text = "Saved fee details successfully at " + DateTime.Now.ToString("dd/MMM/yyyy hh:mm tt");
            }

        }
    }

    private void ClearFDFields()
    {
        txtFDDescription.Text = string.Empty;
        txtFDAmount.Text = string.Empty;
        txtFDAddAmount.Text = string.Empty;
        txtFDOtherAmount.Text = string.Empty;
        lblSFDKey.Text = "-1";
        lblFDMessage.Text = string.Empty;
    }

    protected void btnFDAddNew_Click(object sender, EventArgs e)
    {
        ClearFDFields();
    }

    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        Page.Validate("StudentFee");
        if (Page.IsValid)
        {
            StudentFeeTable _fee = new StudentFeeTable();
            _fee.StudentFeeKey = Convert.ToInt32(txtStudentFeeKey.Text);
            _fee.StudentFeeCode = txtFeeCode.Text;
            _fee.FeeTypeCode = Convert.ToInt16(ddlFeeTypeCode.Text);
            _fee.EducationPeriodKey = Convert.ToInt32(ddlPeriod.Text);
            _fee.Description = txtDescription.Text;
            _fee.Status = true;

            if (StudentFeeTableManager.Save(_fee) > 0)
            {
                txtStudentFeeKey.Text = _fee.StudentFeeKey.ToString();
                //ClearFields();
                grvStudentFee.DataBind();
                lblTopMessage.Text = "Saved successfully at " + DateTime.Now.ToString("dd/MMM/yyyy hh:mm tt");
            }
        }
        else
        {
            lblMessage.Text = "Validation failed.";
        }
    }

    protected void btnAddNew_Click(object sender, EventArgs e)
    {
        ClearFDFields();
        ClearFields();
    }

    private void ClearFields()
    {
        txtFeeCode.Text = string.Empty;
        txtDescription.Text = string.Empty;
        txtStudentFeeKey.Text = "-1";
        lblMessage.Text = string.Empty;
        lblTopMessage.Text = string.Empty;
    }
    protected void ddlSchoolList_SelectedIndexChanged(object sender, EventArgs e)
    {
        ddlFeeTypeCode.DataBind();
        ddlFeeTypeCode_SelectedIndexChanged(sender, e);
    }

    protected void ddlSchoolMajor_SelectedIndexChanged(object sender, EventArgs e)
    {
        ddlStandardLevel.DataBind();
        ddlPeriod.DataBind();
    }

    protected void ddlPeriod_SelectedIndexChanged(object sender, EventArgs e)
    {
        ClearFields();
    }

    protected void ddlStandardLevel_SelectedIndexChanged(object sender, EventArgs e)
    {
        ddlPeriod.DataBind();
        ClearFields();
    }

    protected void ddlLocation_SelectedIndexChanged(object sender, EventArgs e)
    {
        ddlPeriod.DataBind();
        ClearFields();
    }

    protected void ddlFeeTypeCode_SelectedIndexChanged(object sender, EventArgs e)
    {
        grvStudentFee.DataBind();
        ClearFields();
    }

    protected void grvStudentFee_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (grvStudentFee.SelectedIndex >= 0)
        {
            ClearFields();
            ClearFDFields();
            DataKey _selectedDataKey = grvStudentFee.SelectedDataKey;
            int _studentFeeKey = Convert.ToInt32(_selectedDataKey[0]);
            if (_studentFeeKey > 0)
            {
                StudentFeeTable _fee = new StudentFeeTable();
                _fee = StudentFeeTableManager.GetItem(_studentFeeKey);

                if (_fee != null)
                {
                    txtFeeCode.Text = _fee.StudentFeeCode;
                    txtDescription.Text = _fee.Description;
                    txtStudentFeeKey.Text = _fee.StudentFeeKey.ToString();
                    grvStudentFeeDetail.DataBind();
                    tbcEntry.ActiveTab = tbpSFD;
                }
            }
        }
    }

    protected void grvStudentFeeDetail_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (grvStudentFeeDetail.SelectedIndex >= 0)
        {
            ClearFDFields();
            DataKey _selectedDataKey = grvStudentFeeDetail.SelectedDataKey;
            long _sFDKey = Convert.ToInt64(_selectedDataKey[0]);
            int _studentFeeKey = Convert.ToInt32(_selectedDataKey[1]);
            if (_sFDKey > 0 && _studentFeeKey > 0)
            {
                StudentFeeDetail _detail = new StudentFeeDetail();
                _detail = StudentFeeDetailManager.GetItem(_sFDKey, _studentFeeKey);

                if (_detail != null)
                {
                    txtFDDescription.Text = _detail.Description;
                    txtFDAmount.Text = _detail.Amount.ToString("#,000.00");
                    txtFDAddAmount.Text = _detail.AdditionalAmount.ToString("#,000.00");
                    txtFDOtherAmount.Text = _detail.OtherAmount.ToString("#,000.00");
                    lblSFDKey.Text = _detail.SFDKey.ToString();
                    tbcEntry.ActiveTab = tbpSFD;
                }
            }
        }
    }
}
